package com.zsy.service.impl;

import com.zsy.mapper.DeptMapper;
import com.zsy.pojo.Dept;
import com.zsy.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: zhengsuyang
 * @Desc:
 * @create: 2025-07-24 16:16
 **/
@Service
public class DeptServiceImpl  implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 查询所有部门信息
     * @return
     */
    @Override
    public List<Dept> findAll() {
        List<Dept> list = deptMapper.findAll();
        return list;
    }

    /**
     * 根据id查询部门信息
     */
    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.getDeptById(id);
    }

    /**
     * 根据id删除部门
     * @param id
     */
    @Override
    public Integer deleteDeptById(Integer id) {
        Integer count =  deptMapper.selectDeptEmp(id);
        if (count==0){
            deptMapper.deleteDeptById(id);
            return 0;
        }
        else
            return count;
    }

    /**
     * 添加部门
     * @param dept
     */
    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
        System.out.println("添加部门: " + dept);
        System.out.println("添加部门成功");
    }

    /**
     * 修改部门
     * @param dept
     */
    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDept(dept);
        System.out.println("修改部门: " + dept);
    }

}
